/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSBox.h"
#include "NSCell.h"
#include "NSKeyedUnarchiver.h"

IMPLEMENT_DYNAMIC_CLASS(NSBox, NSView)

NSBox::NSBox() : NSView(), m_box(NULL), m_contentView(NULL)
{
}

void NSBox::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSView::initWithUnarchiver(archiver);

	m_contentView = wxDynamicCast(archiver->decodeObjectForKey(wxT("NSContentView")), NSView);
	wxASSERT(m_contentView);
	NSCell* cell = wxDynamicCast(archiver->decodeObjectForKey(wxT("NSTitleCell")), NSCell);
	if (cell)
		m_label = cell->getContents();
}


void NSBox::_releaseWidget(void)
{
	if (m_box)
	{
		m_box->Destroy();
		m_box = NULL;
	}
}

wxWindow* NSBox::_allocWidget(NSView *parent)
{
	wxWindow* pwidget = parent->widget();
	if (pwidget)
	{
		m_box = new wxStaticBox(pwidget, wxID_ANY, m_label);
		wxCHECK(m_box, NULL);
		_initWidget(m_box);
		tile();
	}
	return m_box;
}

void NSBox::setSize(const wxRect& newSize) // NSBox always set client to fully populate its client area
{
	if (_frame == newSize)
		return;

	_frame = newSize;
	if (m_box)
	{
		_initWidget(m_box);
		tile();
	}
}

void NSBox::tile(void)
{
	if (m_contentView)
	{
		wxRect clientSize(m_box->GetClientSize());
		m_contentView->setSize(clientSize);
	}
}
